package com.zhongxi.guoyuan.view.user;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.MyTextUtils;

import java.util.HashMap;
import java.util.Map;

public class FeedBackActivity extends BaseActivity implements View.OnClickListener, TextWatcher {

    private EditText etFeedBack;
    private EditText etInfo;
    private TextView tvSubmit;
    private TextView tvNum;
    private Map<String, String> map = new HashMap<String, String>();

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_feed_back);
        initTitle();
        tvTitle.setText("意见反馈");

        etFeedBack = (EditText) findViewById(R.id.etFeedback);
        etInfo = (EditText) findViewById(R.id.etInfo);
        tvSubmit = (TextView) findViewById(R.id.tvSubmit);
        tvNum = (TextView) findViewById(R.id.tvNum);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        etFeedBack.addTextChangedListener(this);
        tvSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvSubmit:
                //意见反馈接口
                submitJump();
                break;
        }
    }

    private void submitJump() {
        String content = etFeedBack.getText().toString().trim();
        String info = etInfo.getText().toString().trim();
        if(null == content||"".equals(content)){
            showShortToast("意见不能为空");
        }else if(null == info||"".equals(info)){
            showShortToast("手机号/QQ号/邮箱,至少选填一个");
        }else{
            submit(content, info);
        }
    }

    private void submit(String content, String info) {
        map.put("communicationways", info);
        map.put("msg", content);
        getServer(Url.ADD_FEEDBACK, map, "", false, null, "post");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        try {
            Gson gson = new Gson();
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            MyApplication.getInstance().setSpValue(result.getSafeobj());
            showShortToast("反馈成功");
            finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        try{
            Gson gson = new Gson();
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())) {
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable content) {
        MyTextUtils.TextChangeUtils(context, tvNum, content);
    }
}
